package com.aiti.lulian.handler;



import com.alibaba.excel.metadata.Head;


import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.List;

public class HeaderStyleHandler extends AbstractCellWriteHandler {
    @Override
     public void afterCellDispose(
            WriteSheetHolder writeSheetHolder,
            WriteTableHolder writeTableHolder,
            List<WriteCellData<?>> cellDataList,
            Cell cell,
            Head head,
            Integer relativeRowIndex,
            Boolean isHead) {
        // 只处理标题行
        if (isHead) {
            // 设置单元格样式
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle style = workbook.createCellStyle();

            // 设置背景颜色为橙色
            //style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            // 设置居中对齐
            style.setAlignment(HorizontalAlignment.CENTER);

            // 应用样式到单元格
            cell.setCellStyle(style);

            // 设置列宽
            int columnIndex = cell.getColumnIndex();
            writeSheetHolder.getSheet().setColumnWidth(columnIndex, 20 * 256); // 设置每列宽度为30个字符
        }
    }
}

//public class HeaderStyleHandler extends AbstractCellWriteHandler {
//    @Override
//    public void afterCellDispose(
//            WriteSheetHolder writeSheetHolder,
//            WriteTableHolder writeTableHolder,
//            List<WriteCellData<?>> cellDataList,
//            Cell cell,
//            Head head,
//            Integer relativeRowIndex,
//            Boolean isHead) {
//        // 只处理标题行
//        if (isHead) {
//            // 创建并配置单元格样式
//            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
//            CellStyle style = workbook.createCellStyle();
//
//            // 设置背景颜色为橙色，使用 RGB 颜色
//            XSSFColor orangeColor = new XSSFColor(new java.awt.Color(255, 165, 0), null); // RGB for orange
//            if (workbook instanceof XSSFWorkbook) {
//                ((XSSFCellStyle) style).setFillForegroundColor(orangeColor);
//            } else {
//                style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
//            }
//            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//
//            // 设置居中对齐
//            style.setAlignment(HorizontalAlignment.CENTER);
//
//            // 应用样式到单元格
//            cell.setCellStyle(style);
//
//            // 设置列宽
//            int columnIndex = cell.getColumnIndex();
//            SXSSFSheet sheet = (SXSSFSheet) writeSheetHolder.getSheet();
//            sheet.setColumnWidth(columnIndex, 17 * 256); // 设置每列宽度为30个字符
//        }
//    }
//}